#include <linux/init.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/device.h>



ssize_t mybus_show(struct bus_type *bus, char *buf)
{

    return sprintf(buf, "%s\n", "this is mybus_show!");
}

struct bus_attribute mybus_attr = {
    .attr = {
        .name = "value",
        .mode = 0664,
    },
    .show = mybus_show,
};




int mybus_match(struct device *dev, struct device_driver *drv)
{
    // 检查设备名称和驱动程序名称是否匹配
    return (strcmp(dev_name(dev), drv->name) == 0);
}

int mybus_probe(struct device *dev)
{
    struct device_driver *drv = dev->driver;

    if (drv->probe)
    {
        drv->probe(dev);
    }

    return 0;
}


struct bus_type mybus = {
    .name = "mybus",        // 总线的名称
    .match =  mybus_match,  // 设备和驱动程序匹配的回调函数
    .probe = mybus_probe,   // 设备探测的回调函数
};



static int __init mybus_init(void)
{   
    int ret;
   
    ret = bus_register(&mybus);
    if (ret < 0)
    {
        printk(KERN_EMERG "mybus_init error!\n");
        return ret;
    }

    ret = bus_create_file(&mybus, &mybus_attr);
    if (ret < 0)
    {
        printk(KERN_EMERG "bus_create_file error!\n");
        return ret;
    }

    printk(KERN_EMERG "mybus_init OK!\n");
    return 0;
}

static void __exit mybus_exit(void)
{
    bus_remove_file(&mybus, &mybus_attr);   //从sysfs中移除属性文件
    bus_unregister(&mybus);                 //取消注册总线
    printk(KERN_EMERG "mybus_exit OK!\n");  
}



module_init(mybus_init);
module_exit(mybus_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Treasure1029");
